﻿using FRMS.DAL;
using FRMS.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FRMS.BUS
{
    public class BuildBUS
    {
        public ResponseMessage AddBuild(BuildInfo buildInfo)
        {
            ResponseMessage message = new ResponseMessage();
            BuildDAL da = new BuildDAL();
            int count = da.CheckBuildExist(buildInfo.BuildingName,buildInfo.projectID);
            if (count > 0)
            {
                message.errcode = 1;
                message.errmsg = "该楼栋已存在，请勿重复添加";
            }
            else
            {
                bool result = da.AddBuild(buildInfo);
                if (result)
                {
                    message.errcode = 0;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "新增楼栋单元信息失败";
                }
            }
            return message;
        }

        public ResponseMessage EditBuild(BuildInfo buildInfo)
        {
            ResponseMessage message = new ResponseMessage();
            BuildDAL da = new BuildDAL();
            int count = da.CheckBuildExist(buildInfo.BuildingName, buildInfo.projectID);
            if (count > 0)
            {
                message.errcode = 1;
                message.errmsg = "该楼栋已存在，请勿重复添加";
            }
            else
            {
                bool result = da.UpdateBuild(buildInfo);
                if (result)
                {
                    message.errcode = 0;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "修改楼栋单元信息失败";
                }
            }

            return message;
        }

        public ResponseMessage DeleteBuild(BuildInfo buildInfo)
        {
            ResponseMessage message = new ResponseMessage();
            BuildDAL da = new BuildDAL();
            if (da.CheckRoom(buildInfo))
            {
                if (da.CheckElevator(buildInfo))
                {
                    bool result = da.DelBuild(buildInfo);
                    if (result)
                    {
                        message.errcode = 0;
                    }
                    else
                    {
                        message.errcode = 1;
                        message.errmsg = "删除楼栋单元信息失败";
                    }
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "该楼栋单元下绑定电梯信息，请先删除电梯信息";
                }

            }
            else
            {
                message.errcode = 1;
                message.errmsg = "该楼栋单元下绑定房间信息，请先删除房间信息";
            }


            return message;
        }

        public ResponseMessage QueryBuild(BuildInfo buildInfo)
        {
            ResponseMessage message = new ResponseMessage();
            BuildDAL da = new BuildDAL();
            message.errcode = 0;
            message.data = da.QueryBuildInfo(buildInfo);
            message.total = da.QueryBuildInfoCount(buildInfo);

            return message;
        }

        public ResponseMessage LoadBuild(BuildInfo buildInfo)
        {
            ResponseMessage message = new ResponseMessage();
            BuildDAL da = new BuildDAL();
            message.errcode = 0;
            message.data = da.LoadBuild(buildInfo.ID);

            return message;
        }

        public ReturnMessage QueryAllBuild(int projectID)
        {
            ReturnMessage message = new ReturnMessage();
            RoomDAL da = new RoomDAL();
            message.errcode = 0;
            message.data = da.QueryAllRoomInfo(projectID);

            return message;
        }
    }
}
